/* indent size: 2 */

module.exports = app => {
  const DataTypes = app.Sequelize;

  const ResourceProduct = app.model.define('resourceproduct', {
    id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      autoIncrement: true,
      primaryKey: true
    },
    resourceId: {
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true,
      references: {
        model: 'resource',
        key: 'id'
      }
    },
    productId: {
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true,
      references: {
        model: 'product',
        key: 'id'
      }
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: false
    }
  }, {
    tableName: 'resourceproduct',
    timestamps: true, // 设置为 true，Sequelize 将自动生成时间戳
  });

// 关联资源产品
  ResourceProduct.associate = function () {
    // 资源属于产品
    app.model.Resource.belongsToMany(app.model.Product, {
      through: ResourceProduct,
      foreignKey: 'resourceId',//定义联结关系中源模型的 key
      otherKey: 'productId',//定义目标模型中的 key
    });
    // 产品属于资源
    app.model.Product.belongsToMany(app.model.Resource, {
      through: ResourceProduct,
      foreignKey: 'productId',
      otherKey: 'resourceId',
    });
  }

  return ResourceProduct;
};
